<template>
  <div class="comment-post">
    <van-field class="post-field" v-model.trim="message" rows="2" autosize type="textarea" maxlength="50" placeholder="请输入留言" show-word-limit />
    <van-button :disabled="!message" class="post-btn" @click="post">发布</van-button>
  </div>
</template>

<script>
import { PostComment } from '@/api/article.js'
import { Toast } from 'vant'

export default {
  name: 'CommentPost',
  components: {},
  props: {
    target: {
      type: [String, Number, Object],
      required: true
    },
    // 父组件没有传递的话，则是对文章进行评论，不是必须要传的
    artId: {
      type: [String, Number, Object],
      default: null
    }
  },
  data () {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    // 点击发布
    async post () {
      Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '发布中....'
      })
      try {
        const res = await PostComment({
          target: this.target,
          content: this.message,
          art_id: this.artId
        })
        // console.log(res)
        // console.log(res.new_obj)
        Toast.success('评论成功')
        this.$emit('on-success', res.new_obj)
        this.message = ''
      } catch (err) {
        Toast.fail('评论失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
